# encoding: utf-8
from apps.forms import BaseForm
from wtforms import StringField, IntegerField
from utils import bbscache
from wtforms.validators import Length, EqualTo, Regexp, ValidationError, InputRequired, Email
from apps.front.models import FrontUser


class SignupForm(BaseForm):
    email = StringField(validators=[Email(message='邮箱格式错误！')])
    captcha = StringField(validators=[Length(min=6, max=6, message='请输入正确格式的邮箱验证码！')])
    username = StringField(validators=[Length(6, 50, message='用户名长度在6-50之间！')])
    password1 = StringField(validators=[Length(6 - 20, message='密码长度在6-20之间！'),
                                        Regexp(r'[0-9a-zA-Z_\.]', message='密码格式错误！包括数字大小写字母下划线及英文句号。')])
    password2 = StringField(validators=[EqualTo('password1', message='两次密码不一致！')])
    graph_captcha = StringField(
        validators=[Length(min=4, max=4, message='验证码长度为4！'), Regexp(r'[0-9a-zA-Z]', message='验证码格式错误！')])

    # 检测图形验证码是否正确
    def validate_graph_captcha(self, field):
        graph_captcha = field.data
        graph_captcha_mem = bbscache.get(graph_captcha.lower())
        if not graph_captcha_mem:
            raise ValidationError('图形验证码错误！')

    # 验证手机号码是否已被注册
    # def validate_telephone(self, field):
    #     telephone = field.data
    #     user = FrontUser.query.filter_by(telephone=telephone).first()
    #     if user:
    #         raise ValidationError('手机号码已被注册！')

    # 验证邮箱验证码
    def validate_captcha(self, field):
        captcha = field.data
        email = self.email.data
        captcha_cache = bbscache.get(email)
        if not captcha_cache or captcha_cache.lower() != captcha.lower():
            raise ValidationError('邮箱验证码错误！')


class SigninForm(BaseForm):
    email = StringField(validators=[Email(message='邮箱格式错误！')])
    password = StringField(validators=[Length(6 - 20, message='密码长度在6-20之间！')])
    graph_captcha = StringField(
        validators=[Length(min=4, max=4, message='验证码长度为4！'), Regexp(r'[0-9a-zA-Z]', message='验证码格式错误！')])
    rememberPsw = StringField()

    # 检测图形验证码是否正确
    def validate_graph_captcha(self, field):
        graph_captcha = field.data
        graph_captcha_mem = bbscache.get(graph_captcha.lower())
        if not graph_captcha_mem:
            raise ValidationError('验证码错误！')


class AddPostForm(BaseForm):
    title = StringField(validators=[InputRequired('请输入帖子标题！')])
    content = StringField(validators=[InputRequired('请输入帖子内容！')])
    board_id = IntegerField(validators=[InputRequired('请选择帖子所属板块！')])


class AddCommentForm(BaseForm):
    content = StringField(validators=[InputRequired('请输入评论内容！')])
    post_id = StringField(validators=[InputRequired('请传入帖子id！')])


class F_FoundPwdForm(BaseForm):
    email = StringField(validators=[Email('邮箱格式错误！')])
    captcha = StringField(validators=[Length(min=6, max=6, message='请输入正确格式的验证码！')])
    password = StringField(validators=[Length(min=6, max=20, message='密码格式错误！')])

    def validate_captcha(self, field):
        captcha = field.data
        email = self.email.data
        captcha_cache = bbscache.get(email)
        if not captcha_cache or captcha_cache.lower() != captcha.lower():
            raise ValidationError('邮箱验证码错误！')
